<?php

namespace App\Service\OpenShop\Feishu;

use App\Exceptions\Custom\CustomException;
use App\Http\Resources\OpenShop\ShopOrder\OrderLogResource;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;

class OrderLogService
{
    /**
     * 订单日志分页列表
     * @param int $orderId
     * @param int $pageSize
     * @param int $pageNum
     * @return array
     */
    public static function paginateList(int $orderId, int $pageSize, int $pageNum): array
    {
        $paginator = OpenShopSignOrderLogModel::query()
            ->where('orderId', $orderId)
            ->orderBy('id', 'desc')
            ->paginate($pageSize, ['*'], 'pageNum', $pageNum);
        return [
            'list'     => OrderLogResource::collection($paginator->items()),
            'count'    => $paginator->total(),
            'pageNum'  => $pageNum,
            'pageSize' => $pageSize,
        ];
    }

    private static function getChanges($type, string $changes)
    {
        switch ($type){
            case OpenShopSignOrderLogModel::UPLOAD_ORDER_PAY_PROOF:
                $changes = json_decode($changes, true);
                $changes["proofImg"] = privateFilePath($changes["proofImg"]);
                $changes = json_encode($changes, JSON_UNESCAPED_UNICODE);
                break;
            case OpenShopSignOrderLogModel::SELECTION_SUBMIT_DISCLOSURE:
            case OpenShopSignOrderLogModel::UPDATED_SUBMIT_DISCLOSURE:
                $changesTemp = json_decode($changes, true);
                $changes = [
                    'old' => [],
                    'new' => [],
                ];
                foreach (($changesTemp['old'] ?? []) as $item) {
                    $changes['old'][] = privateFilePath($item);
                }
                foreach (($changesTemp['new'] ?? []) as $item) {
                    $changes['new'][] = privateFilePath($item);
                }
                $changes = json_encode($changes, JSON_UNESCAPED_UNICODE);
                break;
        }
        return $changes;
    }
}
